package org.example.mybatis.spring.config;

import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

/**
 * 文件描述
 *
 * @author yezhou41127
 * @date: 2022年11月03日
 */

@MapperScan("org.example.mybatis.spring.mapper.*")
@ComponentScan
@Configuration
public class MainConfig {

	@Bean
	public DataSource dataSource() {
		String url = "jdbc:mysql://localhost:13306/gin-server";
		String userName = "root";
		String password = "roottt";

		HikariConfig hikariConfig = new HikariConfig();
		hikariConfig.setJdbcUrl(url);
		hikariConfig.setUsername(userName);
		hikariConfig.setPassword(password);

		return new HikariDataSource(hikariConfig);
	}

	@Bean
	public SqlSessionFactory sqlSessionFactory() throws Exception {
		SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
		factoryBean.setDataSource(dataSource());
		org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();
		return factoryBean.getObject();
	}
}

